package com.atguigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class TestScopeController {

    @RequestMapping("/testServletAPI")
    /*
    使用ServletAPI方法
     */
    public String testServletAPI(HttpServletRequest request){

        request.setAttribute("testServletAPI","hello，testServletAPI");

        return "success";
    }

    @RequestMapping("/testMVC")
    /*
    使用ModelAndView方法的返回值必须是ModelAndView
     */
    public ModelAndView testMAV(){

        ModelAndView modelAndView = new ModelAndView();
        /*
        ModelAndView包含Model和View的功能
        Model:向请求域中共享对象
        View:设置逻辑视图实现页面跳转
         */
        //向请求域中共享对象
        modelAndView.addObject("testMAV","hello，testMAV");
        //设置逻辑视图
        modelAndView.setViewName("success");

        return modelAndView;
    }


    @RequestMapping("/testModel")

    public String testModel(Model model){
        model.addAttribute("testModel","hello,testModel");
        return "success";
    }

    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("testModelMap","hello,testModelMap");
        return "success";
    }

    @RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("testMap","hello,testMap");
        return "success";
    }

    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("testSession","hello,Session");
        return "success";
    }

    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("testApplication","hello,application");
        return "success";
    }

}
